package com.yztstudy.Controller;

import com.yztstudy.Service.OrderService;
import com.yztstudy.param.CartListParam;
import com.yztstudy.param.OrderParam;
import com.yztstudy.param.PageParam;
import com.yztstudy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 订单的表示层
 */

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired(required = false)
    private OrderService orderService;

    /**
     * TODO 保存订单
     * @param orderParam 前端发来的用户主键和多个商品
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody OrderParam orderParam)
    {
        return orderService.save(orderParam);
    }


    @PostMapping("/list")
    public R list(@RequestBody @Validated CartListParam cartListParam, BindingResult result)
    {
        // TODO 1.如果校验失败
        if (result.hasErrors())
        {
            return R.fail("参数异常,查询失败!");
        }

        // TODO 2.没有问题，就正常调用业务
        return orderService.list(cartListParam);
    }


    /**
     * TODO 检查订单里面是否有要删除的商品
     * @param productId 要删除的商品Id
     * @return
     */
    @PostMapping("/check")
    public R check(@RequestBody Integer productId)
    {
        return orderService.check(productId);
    }


    /**
     * TODO 分页查询订单
     * @param pageParam 分页参数
     * @return
     */
    @GetMapping("/admin/list")
    public R adminList(@RequestBody PageParam pageParam)
    {
        return orderService.adminList(pageParam);
    }
}
